package com.test.fundible.fundible_07_loan_status;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.test.fundible.R;
import com.test.fundible.fundible_00_customer_service.Feed_fundiblebackActivity;
import com.test.fundible.fundible_01_launcher.App_fundibleShow_fundibleCustomize;
import com.test.fundible.fundible_00_utils.FundibleConfigs;
import com.test.fundible.fundible_00_analytics.FundibleUtil.FundibleApRvUtil;
import com.test.fundible.fundible_00_http.OkHttpUtil;
import com.test.fundible.fundible_00_utils.FundibleUtil;

public class Rate_fundibleActive_fundibleDialog extends Dialog {
    
    private final String url_fundible = "https://play.google.com/store/apps/details?id=" + FundibleConfigs.PACK_fundibleAGE_NAME;
    private int score_fundible = 0;
    private static boolean isShow_fundible = false;
    private final ImageButton[] starIbs_fundible = new ImageButton[5];

    private final String page_fundible = "DIALOG_REVIEW_ACTIVITE";

    private final AppCompatActivity activity_fundible;

    public Rate_fundibleActive_fundibleDialog(AppCompatActivity activity){
        super(activity);
        this.activity_fundible = activity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_fundiblerate_active);

        setCancelable(false);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setCanceledOnTouchOutside(false);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        getWindow().setBackgroundDrawableResource(R.color.transparent);


        setCancelable(false);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setCanceledOnTouchOutside(false);

        findViewById(R.id.draa_btn_notnow).setOnClickListener(v -> {
            OkHttpUtil.getInstance_fundible().uploadEventPoint(page_fundible,"CANCEL_CLICK",String.valueOf(score_fundible));
            dismiss();
        });

        TextView textTv = findViewById(R.id.draa_tv_desc);

        String content = RateAc_fundibletiveWind_fundibleowConfig.getInstance().getContent_fundible();
        if (!TextUtils.isEmpty(content)) textTv.setText(content);

        starIbs_fundible[0] = findViewById(R.id.draa_ib_star_1);
        starIbs_fundible[1] = findViewById(R.id.draa_ib_star_2);
        starIbs_fundible[2] = findViewById(R.id.draa_ib_star_3);
        starIbs_fundible[3] = findViewById(R.id.draa_ib_star_4);
        starIbs_fundible[4] = findViewById(R.id.draa_ib_star_5);

        findViewById(R.id.draa_btn_rate).setOnClickListener(v -> {

            OkHttpUtil.getInstance_fundible().uploadEventPoint(page_fundible,"RATE_CLICK",String.valueOf(score_fundible));

            if (score_fundible ==0) {
                Toast.makeText(activity_fundible, activity_fundible.getString(R.string.tips_please_rate_fundible),Toast.LENGTH_SHORT).show();
                return;
            }

            if (score_fundible >=4){
                if (App_fundibleShow_fundibleCustomize.getInstance_fundible().showSysReview_fundible()){
                    FundibleApRvUtil.INSTANCE.launchRvFlow_fundible(activity_fundible, aBoolean -> {
                        if (!aBoolean) activity_fundible.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url_fundible)));
                        dismiss();
                        return null;
                    });
                }else activity_fundible.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url_fundible)));


            }else activity_fundible.startActivity(new Intent(activity_fundible, Feed_fundiblebackActivity.class));

            dismiss();
        });

        for (int i = 0; i< starIbs_fundible.length; i++){
            ImageButton scoreIb = starIbs_fundible[i];
            final int fScore = i+1;
            if (scoreIb!=null) scoreIb.setOnClickListener(v -> setScore_fundible(fScore));
        }

    }

    private void setScore_fundible(int score_fundible){

        if (score_fundible >5) score_fundible =5;
        else if (score_fundible <1) score_fundible =1;

        this.score_fundible = score_fundible;

        OkHttpUtil.getInstance_fundible().uploadEventPoint(page_fundible,"STAR_CLICK",String.valueOf(score_fundible));

        for (int i = 0; i< starIbs_fundible.length; i++){
            ImageButton scoreIb = starIbs_fundible[i];
            if (scoreIb==null) continue;
            scoreIb.setBackgroundResource(i< score_fundible ? R.drawable.icon_rate_star_light :R.drawable.icon_rate_star_gray);
        }
    }

    @Override
    public void show() {
        if (isShow_fundible) return;
        isShow_fundible = true;
        super.show();
        OkHttpUtil.getInstance_fundible().uploadEventPoint(page_fundible,"START","");
        FundibleUtil.addActivedialogCount_fundible();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        OkHttpUtil.getInstance_fundible().uploadEventPoint(page_fundible,"END","");
        isShow_fundible = false;
    }


}
